import { getBaseCategoryList, getBanners, getFloors } from "api/api"
import store from "../store";
export default {
    state: {
        categoryList: [],
        banners: [],
        floors: []
    },
    getters: {
        //对floors响应的数据进行处理，得到需要的相应结构的数据,也可以在相应拦截当中做
        // filterFloors(state) {
        //     let floors = state.floors;

        //     floors.forEach(floor => {
        //         if (floor.carouselList) {
        //             floor.carouselList = floor.carouselList.map(({ id, imgUrl }) => ({
        //                 id,
        //                 url: imgUrl
        //             }))
        //         }
        //     })

        //     return floors;
        // }
    },
    mutations: {
        getCategoryList(state, categoryList) {
            state.categoryList = categoryList
        },
        getBanners(state, banners) {
            state.banners = banners
        },
        getFloors(state, floors) {
            state.floors = floors
        }
    },
    actions: {
        async getCategoryList(store) {
            const { code, data: categoryList } = await getBaseCategoryList();
            try {
                if (code == 200) {
                    //获取三级列表信息数据
                    store.commit('getCategoryList', categoryList)
                } else {
                    //目前else不需要处理，可以给用户提示信息
                }
                return { code, data: categoryList }
            } catch (error) {
                return Promise.reject(error)
            }
        },
        async getBanners(store) {
            const { code, data: banners } = await getBanners();
            try {
                if (code == 200) {
                    //获取三级列表信息数据
                    store.commit('getBanners', banners)
                } else {
                    //目前else不需要处理，可以给用户提示信息
                }
                return { code, data: banners }
            } catch (error) {
                return Promise.reject(error)
            }
        },
        async getFloors(store) {
            const { code, data: floors } = await getFloors();
            try {
                if (code == 200) {
                    //获取三级列表信息数据
                    store.commit('getFloors', floors)
                } else {
                    //目前else不需要处理，可以给用户提示信息
                }
                return { code, data: floors }
            } catch (error) {
                return Promise.reject(error)
            }
        }
    }
}
